package com.demo.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo.model.JsonModel;
import com.demo.model.Project;
import com.demo.model.User;
import com.demo.service.ProjectService;
import com.demo.service.UserService;
import com.demo.util.EscapeUnescape;
 
@Controller
@RequestMapping("/project")
public class ProjectController {
 
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private UserService userService;
	
	 @RequestMapping(value="/projectList.do")
	 public String selectAll(Model model){
	    	List<Project> projectList = projectService.selectAll();
	    	List<Project> projectListFenye = new ArrayList<Project>();
	    	int count = projectList.size();
	    	int n = (count>=5) ? 5:count;
	    	for(int i = 0;i < n;i++){
	    		projectListFenye.add(projectList.get(i));
	    	}
	    	int zongye = 0;
	    	if(count%5 == 0){
	    		zongye = count/5;
	    	}else{
	    		zongye = count/5+1;
	    	}
	    	int dangqian = 1;
	    	
	    	model.addAttribute("projectList", projectListFenye);
	    	model.addAttribute("count", count);
	    	model.addAttribute("zongye", zongye);
	    	model.addAttribute("dangqian", dangqian);

			return "project/projectList";
	  }
	 @RequestMapping(value="/selectProjectFY.do")
	 public String selectProjectFY(Integer dangqian,Integer sx,Model model){
	    	List<Project> projectList = projectService.selectAll();
	    	List<Project> projectListFenye = new ArrayList<Project>();
	    	
	    	int count = projectList.size();
	    	int zongye = 0;
	    	if(count%5 == 0){
	    		zongye = count/5;
	    	}else{
	    		zongye = count/5+1;
	    	}
	    	if(sx == 1){ 			//上一页
    			dangqian = dangqian - 1;
    		}else if(sx == 2){       //下一页
    			dangqian = dangqian + 1;
    			if(dangqian == zongye);
    			dangqian = -1;
    		}
	    	
	    	int n = (count>=5) ? 5:count;
	    	dangqian = (count>=5) ? dangqian:0;
	    	
	    	if(dangqian == 0){
	    		for(int i = 0;i < n;i++){
		    		projectListFenye.add(projectList.get(i));
		    	}
	    		
		    	dangqian = 1;
	    	}else if(dangqian == -1){
	    		if(count%5 == 0){
		    		zongye = count/5;
		    	}else{
		    		zongye = count/5+1;
		    	}
	    		for(int i = (zongye-1)*5;i < count-1;i++){
		    		projectListFenye.add(projectList.get(i));
		    	}
		    	dangqian = zongye;
	    	}else{
	    		for(int i = (dangqian - 1)*5;i < dangqian*5;i++){
		    		projectListFenye.add(projectList.get(i));
		    	}
	    	}
	    	
	    	model.addAttribute("projectList", projectListFenye);
	    	model.addAttribute("count", count);
	    	model.addAttribute("zongye", zongye);
	    	model.addAttribute("dangqian", dangqian);

			return "project/projectList";
	  }
	 @RequestMapping(value="/selectLike.do")
	 public String selectLike(Project project,Model model){
	    	List<Project> projectList = projectService.selectLike(project.getName(),project.getFuzeren());
	    	List<Project> projectListFenye = new ArrayList<Project>();
	    	int count = projectList.size();
	    	int n = (count>=5) ? 5:count;
	    	for(int i = 0;i < n;i++){
	    		projectListFenye.add(projectList.get(i));
	    	}
	    	int zongye = 0;
	    	if(count%5 == 0){
	    		zongye = count/5;
	    	}else{
	    		zongye = count/5+1;
	    	}
	    	int dangqian = 1;
	    	
	    	model.addAttribute("projectList", projectListFenye);
	    	model.addAttribute("count", count);
	    	model.addAttribute("zongye", zongye);
	    	model.addAttribute("dangqian", dangqian);
			return "project/projectList";
	  }
	 @RequestMapping(value="/toAddProject.do")
	 public String toAddProject(Model model){
		    List<User> userList = userService.selectAll();
		    model.addAttribute("userList", userList);
			return "project/addProject";
	  }
	 
	 @RequestMapping(value="/addProject.do")
	 public String addProject(Project project,Model model){
		    int n = projectService.insert(project);
		    
	    	List<Project> projectList = projectService.selectAll();
	    	model.addAttribute("projectList", projectList);
	    	
			return "redirect:projectList.do";
	  }
	 
	 @RequestMapping(value="/toUpdateProject.do")
	 public String toUpdateProject(Integer id,Model model){
		 	Project project = projectService.selectById(id);
		 	model.addAttribute("project", project);
		 	
		 	List<User> userList = userService.selectAll();
		    model.addAttribute("userList", userList);
		    
			return "project/updateProject";
	  }
	 @RequestMapping(value="/updateProject.do")
	 public String updateProject(Project project,Model model){
	
		    int n = projectService.updateByPrimaryKey(project);
		    
	    	List<Project> projectList = projectService.selectAll();
	    	model.addAttribute("projectList", projectList);
			return "redirect:projectList.do";
	  }
	 
	 @RequestMapping(value="/deleteProject.do")
	 public String deleteProject(Integer id,Model model){
		 	int n = projectService.deleteByPrimaryKey(id);

		 	List<Project> projectList = projectService.selectAll();
	    	model.addAttribute("projectList", projectList);
			return "redirect:projectList.do";
	  }
 
	    @InitBinder
	    protected void initBinder(WebDataBinder binder) {
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	    }
	  
}